var userPrivilege={
    init:function(){
        var self=this;
        self.shadeDiv = $("#shade-div");
        self.bindEvents();

    },
    bindEvents:function(){
        var self=this;

//        添加权限
        $("#addRights").click(function(){
        	$("#save_name").val("");
    		$("#save_parameter").val("");
    		$("#save_description").val("");
            self.shadeDiv.show();
            $("#newlyAdded").setPosition().show();
        });
        $("#saveButton").click(function(){
        	userPrivilege.ajaxPOST(ctx+"/privilege/user_privilege!save.xhtml",$("#saveForm").serialize(),function(data){
        		common.refreshPage();
        	});
        });        
        
//        修改权限
        $(".edit").click(function(){
        	var spid=$(this).attr("value");
        	//加载用户数据
        	userPrivilege.ajaxPOST(ctx+"/privilege/user_privilege!edit.xhtml","syspower.id="+spid,function(data){
        		$("#edit_id").val(data.id);
        		$("#edit_name").val(data.name);
        		$("#edit_parameter").val(data.parameter);
        		$("#edit_description").val(data.description);
        		//common.refreshPage();
        	});
            self.shadeDiv.show();
            $("#editRights").setPosition().show();
        });
        $("#editButton").click(function(){
        	userPrivilege.ajaxPOST(ctx+"/privilege/user_privilege!update.xhtml",$("#editForm").serialize(),function(data){
        		common.refreshPage();
        	});
        });
        
//        删除
        $(".delete").click(function(){
        	$("#sureDelId").val($(this).attr("value"));
            self.shadeDiv.show();
            $('#warning').setPosition().show().find("p.center").html("确定要删除吗？");

        });
        //确定删除
        $("#sureDelButton").click(function(){
        	userPrivilege.ajaxPOST(ctx+"/privilege/user_privilege!delete.xhtml","syspower.id="+$("#sureDelId").val(),function(data){
        		common.refreshPage();
        	});
        });

    },

    ajaxGET:function (url, data, fn,beforeSendFn) {
        var beforeSendFn = beforeSendFn || function(){};
        $.ajax({type:"GET", dataType:'json', context:this, url:url, data:data, success:fn, beforeSend:beforeSendFn});
    },
    ajaxPOST:function (url, data, fn,beforeSendFn) {
        var beforeSendFn = beforeSendFn || function(){};
        $.ajax({type:"POST", dataType:'json', context:this, url:url, data:data, success:fn, beforeSend:beforeSendFn});
    }

}
$(function(){
	userPrivilege.init();
})